package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.taglibs.standard.lang.jstl.test.beans.PublicInterface2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.entity.Consumer;
import com.google.gson.Gson;
import com.servers.ConsumerServers;

@Controller
public class PersonController {
	@Autowired 
	private ConsumerServers consumerServers;
	
	@RequestMapping("/gopersoninfo")
	public ModelAndView gopersoninfo(HttpServletRequest request) {
		ModelAndView modelAndView=new ModelAndView();
		Consumer attribute = (Consumer) request.getSession().getAttribute("consumer");
		modelAndView.setViewName("personinfomotion");;
		modelAndView.addObject("consumer", attribute);
		return modelAndView;
	}
	@RequestMapping("/updateConsumerInfo")
	public ModelAndView updateConsumerInfo(Consumer consumer ,String id) {
		int cid=Integer.parseInt(id);
		consumer.setCid(cid);
		System.out.println(consumer+"@@@@@@@@@@@@@@@@@@@@"+","+id);
		int updateConsumer = consumerServers.updateConsumer(consumer);  // 修改consumer
		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+updateConsumer);
		ModelAndView modelAndView=new ModelAndView();
		//Consumer attribute = (Consumer) request.getSession().getAttribute("consumer");
		modelAndView.setViewName("personinfomotion");;
	//	modelAndView.addObject("consumer", attribute);
		return modelAndView;
	}
	
	@RequestMapping("/updatepwd")
	public String updatepwd() {
		
		return "personpassword";
	}
	@RequestMapping("/updateConsumePwd")
	public String updateConsumePwd(String newpwd,HttpServletRequest request,String beanpwd) {
		System.out.println(newpwd+"---------------进入到修改密码------------------"+beanpwd);
		
		Consumer consumer=(Consumer) request.getSession().getAttribute("consumer");
		if (beanpwd.trim().equals(consumer.getCpwd())) {
			
			consumerServers.updateConsumePwd(consumer.getCid(), newpwd);
			request.setAttribute("successmsg", "修改成功,下次记得使用密码登录哦");
			return "personpassword";
		}else {
			request.setAttribute("successmsg", "修改失败,原密码错误请重新输入");
			return "personpassword";
		}
		
	}
	@ResponseBody
	@RequestMapping("/findpwdByid")
	public String findpwdByid(String pwd,HttpServletRequest request) {
		System.out.println("---------------进入到查询员密码是否正确------------------");
		Map< String, Object> map=new HashMap<>();
		Gson gson=new Gson();
		Consumer consumer=(Consumer) request.getSession().getAttribute("consumer");
		if (consumer.getCpwd().equals(pwd)) {
			//request.setAttribute("success", "对应");
			map.put("msgs", "success");
		}else  {
			map.put("msgs", "error");
		}
		return gson.toJson(map);
	}
	
	
	public void setConsumerServers(ConsumerServers consumerServers) {
		this.consumerServers = consumerServers;
	}
	
	
	
}
